{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "df1f8fcc-6706-4b0f-bf49-88f583809dae",
   "metadata": {},
   "source": [
    "# Python基础教程\n",
    "\n",
    "## Python基础\n",
    "\n",
    "### 解释器和脚本文件\n",
    "\n",
    "Python代码可以通过解释器直接运行命令，或者保存在脚本文件中运行。脚本文件通常以`.py`为后缀。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fb6d44e9-5d4d-4194-9bbf-7f71b4f96953",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "# 示例代码\n",
    "print(\"Hello, Python!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f07aefd-be03-4b2c-b437-4e8592ae380b",
   "metadata": {},
   "source": [
    "#### 练习题：\n",
    "- 打开Python解释器，输入`print(\"Hello, Python!\")`，观察输出。\n",
    "- 创建一个名为`hello.py`的脚本文件，写入同样的`print`语句，然后通过命令行运行这个脚本。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fe59cce7-9cc4-499e-a817-bb3f09d5c01f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "!python hello.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fadf0860-90c6-4cc3-a638-9f3d9bfb4798",
   "metadata": {},
   "source": [
    "### 基本语法\n",
    "\n",
    "#### 注释\n",
    "使用`#`来添加单行注释。\n",
    "\n",
    "#### 缩进\n",
    "Python使用缩进来定义代码的块结构。\n",
    "\n",
    "#### 变量命名规则\n",
    "变量名可以包含字母、数字、下划线，但不能以数字开头。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6dc14eda-bb95-4aa1-b984-3a35f0f994e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "# 示例代码\n",
    "# 这是一个注释\n",
    "if True:\n",
    "    variable_name = \"Hello, Python!\"\n",
    "    print(variable_name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "425fb7a6-ad4b-48ce-9723-4f2278b35c8e",
   "metadata": {},
   "source": [
    "#### 练习题：\n",
    "- 写一个Python脚本，包含至少一个变量赋值，一个条件语句，和一个注释。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3589a97a-52c2-40ef-bb97-48a93725aa26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "# 这是一个注释\n",
    "variable_name = \"Hello, Python!\"\n",
    "if variable_name:\n",
    "    print(variable_name)  # 条件为真时打印变量内容\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ae623d5-73cb-44c7-a8ac-0bcb861f1724",
   "metadata": {},
   "source": [
    "### 数据类型\n",
    "\n",
    "#### 数字（整数和浮点数）\n",
    "整数和浮点数是Python中的基本数值类型。\n",
    "\n",
    "#### 字符串\n",
    "字符串是由字符组成的序列，用单引号或双引号括起来。\n",
    "\n",
    "#### 布尔值\n",
    "布尔值代表真值`True`和假值`False`。\n",
    "\n",
    "#### 复数\n",
    "复数是由实部和虚部组成的数，在Python中可以使用`j`来表示虚部。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "94f3789c-9028-44eb-86e8-de319747c805",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 10.5 Hello, Python! True (3+4j)\n"
     ]
    }
   ],
   "source": [
    "# 示例代码\n",
    "integer_example = 10\n",
    "float_example = 10.5\n",
    "string_example = \"Hello, Python!\"\n",
    "boolean_example = True\n",
    "complex_example = 3 + 4j\n",
    "print(integer_example, float_example, string_example, boolean_example,complex_example)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6b48498-3cb3-4a09-8cdc-af738e13c016",
   "metadata": {},
   "source": [
    "#### 练习题：\n",
    "- 创建几个不同类型的变量（整数、浮点数、字符串、布尔值、复数），并使用`print`语句输出它们。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd6a3ac-ca54-49bb-a6a6-3571494dfc07",
   "metadata": {},
   "source": [
    "## 基本数据结构\n",
    "\n",
    "### 列表（List）\n",
    "\n",
    "列表是Python中用于存储元素序列的数据结构，元素可以是不同类型的。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b1635ee5-3494-46b5-b24c-76f08e7d6301",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表中的所有元素:\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "在列表末尾添加6之后的列表: [1, 2, 3, 4, 5, 6]\n",
      "在索引2处插入10之后的列表: [1, 2, 10, 3, 4, 5, 6]\n",
      "删除元素3之后的列表: [1, 2, 10, 4, 5, 6]\n",
      "被弹出的元素: 6\n",
      "弹出元素之后的列表: [1, 2, 10, 4, 5]\n",
      "列表的长度: 5\n",
      "索引1处的元素: 2\n",
      "从索引1到3的切片: [2, 10, 4]\n",
      "每个元素平方之后的新列表: [1, 4, 100, 16, 25]\n",
      "清空列表之后的列表: []\n"
     ]
    }
   ],
   "source": [
    "# 创建一个列表\n",
    "my_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "# 打印列表中的所有元素\n",
    "print(\"列表中的所有元素:\")\n",
    "for item in my_list:\n",
    "    print(item)\n",
    "\n",
    "# 添加一个元素到列表末尾\n",
    "my_list.append(6)\n",
    "print(\"在列表末尾添加6之后的列表:\", my_list)\n",
    "\n",
    "# 插入一个元素到指定位置\n",
    "my_list.insert(2, 10)\n",
    "print(\"在索引2处插入10之后的列表:\", my_list)\n",
    "\n",
    "# 删除列表中的一个元素\n",
    "my_list.remove(3)\n",
    "print(\"删除元素3之后的列表:\", my_list)\n",
    "\n",
    "# 弹出列表中的最后一个元素\n",
    "popped_element = my_list.pop()\n",
    "print(\"被弹出的元素:\", popped_element)\n",
    "print(\"弹出元素之后的列表:\", my_list)\n",
    "\n",
    "# 获取列表长度\n",
    "list_length = len(my_list)\n",
    "print(\"列表的长度:\", list_length)\n",
    "\n",
    "# 访问列表中的元素\n",
    "print(\"索引1处的元素:\", my_list[1])\n",
    "\n",
    "# 切片操作\n",
    "print(\"从索引1到3的切片:\", my_list[1:4])\n",
    "\n",
    "# 使用列表解析创建新列表\n",
    "squared_list = [x**2 for x in my_list]\n",
    "print(\"每个元素平方之后的新列表:\", squared_list)\n",
    "\n",
    "# 清空列表\n",
    "my_list.clear()\n",
    "print(\"清空列表之后的列表:\", my_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d7c6e605-9a90-4067-8894-162c6811f08b",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_list = [1, 2, 3, 4, 5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "40e61aec-a2cc-42d0-b5fe-4d71ca31e3a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "530b5ceb-8bef-46f6-81ac-1955717c9ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "squared_list = [x**2 for x in my_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d3f50575-9840-4aca-be1a-18f27c7fed71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squared_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa11353a-2d60-47ac-acbd-861ca3123dde",
   "metadata": {},
   "source": [
    "### 元组（Tuple）\n",
    "\n",
    "元组与列表类似，但是一旦创建就不能被修改（不可变性）。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4c1635ca-cfa6-4263-ae2d-78c361d07526",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元组元素:\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "第2个索引处的元素: 3\n",
      "从索引1到3的切片元组: (2, 3, 4)\n",
      "元组长度: 5\n",
      "解包后的元素: 1 2 3 4 5\n"
     ]
    }
   ],
   "source": [
    "# 创建一个元组\n",
    "my_tuple = (1, 2, 3, 4, 5)\n",
    "\n",
    "# 打印元组中的所有元素\n",
    "print(\"元组元素:\")\n",
    "for item in my_tuple:\n",
    "    print(item)\n",
    "\n",
    "# 访问元组中的元素\n",
    "print(\"第2个索引处的元素:\", my_tuple[2])\n",
    "\n",
    "# 元组切片操作\n",
    "print(\"从索引1到3的切片元组:\", my_tuple[1:4])\n",
    "\n",
    "# 获取元组长度\n",
    "tuple_length = len(my_tuple)\n",
    "print(\"元组长度:\", tuple_length)\n",
    "\n",
    "# 元组解包\n",
    "a, b, c, d, e = my_tuple\n",
    "print(\"解包后的元素:\", a, b, c, d, e)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2ebe4d15-e7d1-4089-9992-f43ca119a1e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字典元素:\n",
      "name : John\n",
      "age : 30\n",
      "city : New York\n",
      "姓名: John\n",
      "年龄: 30\n",
      "添加新的键值对后的字典: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}\n",
      "更新年龄后的字典: {'name': 'John', 'age': 35, 'city': 'New York', 'job': 'Engineer'}\n",
      "删除城市后的字典: {'name': 'John', 'age': 35, 'job': 'Engineer'}\n",
      "年龄存在于字典中\n",
      "字典长度: 3\n"
     ]
    }
   ],
   "source": [
    "# 创建一个字典\n",
    "my_dict = {\n",
    "    \"name\": \"John\",\n",
    "    \"age\": 30,\n",
    "    \"city\": \"New York\"\n",
    "}\n",
    "\n",
    "# 打印字典中的所有键值对\n",
    "print(\"字典元素:\")\n",
    "for key, value in my_dict.items():\n",
    "    print(key, \":\", value)\n",
    "\n",
    "# 访问字典中的值\n",
    "print(\"姓名:\", my_dict[\"name\"])\n",
    "print(\"年龄:\", my_dict[\"age\"])\n",
    "\n",
    "# 添加新的键值对\n",
    "my_dict[\"job\"] = \"Engineer\"\n",
    "print(\"添加新的键值对后的字典:\", my_dict)\n",
    "\n",
    "# 修改现有键的值\n",
    "my_dict[\"age\"] = 35\n",
    "print(\"更新年龄后的字典:\", my_dict)\n",
    "\n",
    "# 删除一个键值对\n",
    "del my_dict[\"city\"]\n",
    "print(\"删除城市后的字典:\", my_dict)\n",
    "\n",
    "# 检查键是否存在\n",
    "if \"age\" in my_dict:\n",
    "    print(\"年龄存在于字典中\")\n",
    "\n",
    "# 获取字典长度\n",
    "dict_length = len(my_dict)\n",
    "print(\"字典长度:\", dict_length)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae7ec719-ebfe-46ae-89be-b1d32d1dedf5",
   "metadata": {},
   "source": [
    "### 集合（Set）\n",
    "\n",
    "集合是一个无序的元素集，自动去除重复元素。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a4c0f8cb-b35b-4ed4-a9f8-a54a96efcb75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "集合元素:\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "添加元素6后的集合: {1, 2, 3, 4, 5, 6}\n",
      "移除元素3后的集合: {1, 2, 4, 5, 6}\n",
      "集合1和集合2的并集: {1, 2, 3, 4, 5}\n",
      "集合1和集合2的交集: {3}\n",
      "集合1相对于集合2的差集: {1, 2}\n",
      "元素4存在于集合中\n",
      "集合长度: 5\n"
     ]
    }
   ],
   "source": [
    "# 创建一个集合\n",
    "my_set = {1, 2, 3, 4, 5}\n",
    "\n",
    "# 打印集合中的所有元素\n",
    "print(\"集合元素:\")\n",
    "for item in my_set:\n",
    "    print(item)\n",
    "\n",
    "# 向集合中添加一个元素\n",
    "my_set.add(6)\n",
    "print(\"添加元素6后的集合:\", my_set)\n",
    "\n",
    "# 从集合中移除一个元素\n",
    "my_set.remove(3)\n",
    "print(\"移除元素3后的集合:\", my_set)\n",
    "\n",
    "# 集合运算 - 并集\n",
    "set1 = {1, 2, 3}\n",
    "set2 = {3, 4, 5}\n",
    "union_set = set1 | set2\n",
    "print(\"集合1和集合2的并集:\", union_set)\n",
    "\n",
    "# 集合运算 - 交集\n",
    "intersection_set = set1 & set2\n",
    "print(\"集合1和集合2的交集:\", intersection_set)\n",
    "\n",
    "# 集合运算 - 差集\n",
    "difference_set = set1 - set2\n",
    "print(\"集合1相对于集合2的差集:\", difference_set)\n",
    "\n",
    "# 检查元素是否存在于集合中\n",
    "if 4 in my_set:\n",
    "    print(\"元素4存在于集合中\")\n",
    "\n",
    "# 获取集合的长度\n",
    "set_length = len(my_set)\n",
    "print(\"集合长度:\", set_length)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9988a6e5-8828-4eb0-b382-abeeef9375f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "整数转换为浮点数: 10.0\n",
      "浮点数转换为整数: 10\n",
      "整数转换为字符串: 10\n",
      "字符串转换为整数: 10\n",
      "字符串转换为列表: ['h', 'e', 'l', 'l', 'o']\n",
      "列表转换为元组: (1, 2, 3, 4, 5)\n",
      "元组转换为列表: [1, 2, 3, 4, 5]\n",
      "字典转换为集合（仅获取键）: {'a', 'b', 'c'}\n",
      "集合转换为列表: [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "# 整数转换为浮点数\n",
    "integer_number = 10\n",
    "float_number = float(integer_number)\n",
    "print(\"整数转换为浮点数:\", float_number)\n",
    "\n",
    "# 浮点数转换为整数\n",
    "float_number = 10.5\n",
    "integer_number = int(float_number)\n",
    "print(\"浮点数转换为整数:\", integer_number)\n",
    "\n",
    "# 整数转换为字符串\n",
    "integer_number = 10\n",
    "string_number = str(integer_number)\n",
    "print(\"整数转换为字符串:\", string_number)\n",
    "\n",
    "# 字符串转换为整数\n",
    "string_number = \"10\"\n",
    "integer_number = int(string_number)\n",
    "print(\"字符串转换为整数:\", integer_number)\n",
    "\n",
    "# 字符串转换为列表\n",
    "string = \"hello\"\n",
    "list_from_string = list(string)\n",
    "print(\"字符串转换为列表:\", list_from_string)\n",
    "\n",
    "# 列表转换为元组\n",
    "my_list = [1, 2, 3, 4, 5]\n",
    "my_tuple = tuple(my_list)\n",
    "print(\"列表转换为元组:\", my_tuple)\n",
    "\n",
    "# 元组转换为列表\n",
    "my_tuple = (1, 2, 3, 4, 5)\n",
    "my_list = list(my_tuple)\n",
    "print(\"元组转换为列表:\", my_list)\n",
    "\n",
    "# 字典转换为集合（仅获取键）\n",
    "my_dict = {\"a\": 1, \"b\": 2, \"c\": 3}\n",
    "my_set = set(my_dict)\n",
    "print(\"字典转换为集合（仅获取键）:\", my_set)\n",
    "\n",
    "# 集合转换为列表\n",
    "my_set = {1, 2, 3, 4, 5}\n",
    "my_list = list(my_set)\n",
    "print(\"集合转换为列表:\", my_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5597fe1-a864-41f5-b7e5-a86fff0e9813",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "70d02ae2-560d-415e-af78-33657cda32f3",
   "metadata": {},
   "source": [
    "### 数据的解构\n",
    "\n",
    "\n",
    "数据的解构是指从一个复合数据结构中提取出其内部的各个元素或部分，并将它们分配给独立的变量。在 Python 中，常见的复合数据结构包括元组、列表和字典。\n",
    "\n",
    "例如，如果有一个元组 (1, 2, 3)，解构就是将这个元组中的每个元素提取出来并赋给独立的变量，比如 a, b, c = (1, 2, 3)，这样 a 就等于 1，b 等于 2，c 等于 3。\n",
    "\n",
    "类似地，对于列表 [4, 5, 6]，解构操作 x, y, z = [4, 5, 6] 将列表中的每个元素分配给相应的变量。\n",
    "\n",
    "而对于字典 { \"name\": \"Alice\", \"age\": 30, \"city\": \"New York\" }，解构键值对可以用 name, age, city = my_dict.items() 的方式来实现，或者也可以单独解构键或值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "74c0efd6-0196-49d5-9033-0fd3741f2410",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元组解构结果:\n",
      "a = 1\n",
      "b = 2\n",
      "c = 3\n",
      "\n",
      "列表解构结果:\n",
      "x = 4\n",
      "y = 5\n",
      "z = 6\n",
      "\n",
      "字典解构键值对结果:\n",
      "name = ('name', 'Alice')\n",
      "age = ('age', 30)\n",
      "city = ('city', 'New York')\n",
      "\n",
      "字典解构键值对的值结果:\n",
      "name = Alice\n",
      "age = 30\n",
      "city = New York\n",
      "\n",
      "字典解构键值对的键结果:\n",
      "key1 = name\n",
      "key2 = age\n",
      "key3 = city\n"
     ]
    }
   ],
   "source": [
    "# 元组解构\n",
    "my_tuple = (1, 2, 3)\n",
    "a, b, c = my_tuple\n",
    "print(\"元组解构结果:\")\n",
    "print(\"a =\", a)\n",
    "print(\"b =\", b)\n",
    "print(\"c =\", c)\n",
    "\n",
    "# 列表解构\n",
    "my_list = [4, 5, 6]\n",
    "x, y, z = my_list\n",
    "print(\"\\n列表解构结果:\")\n",
    "print(\"x =\", x)\n",
    "print(\"y =\", y)\n",
    "print(\"z =\", z)\n",
    "\n",
    "# 字典解构 - 解构键值对\n",
    "my_dict = {\"name\": \"Alice\", \"age\": 30, \"city\": \"New York\"}\n",
    "name, age, city = my_dict.items()\n",
    "print(\"\\n字典解构键值对结果:\")\n",
    "print(\"name =\", name)\n",
    "print(\"age =\", age)\n",
    "print(\"city =\", city)\n",
    "\n",
    "# 字典解构 - 解构键值对的值\n",
    "name, age, city = my_dict.values()\n",
    "print(\"\\n字典解构键值对的值结果:\")\n",
    "print(\"name =\", name)\n",
    "print(\"age =\", age)\n",
    "print(\"city =\", city)\n",
    "\n",
    "# 字典解构 - 解构键值对的键\n",
    "key1, key2, key3 = my_dict.keys()\n",
    "print(\"\\n字典解构键值对的键结果:\")\n",
    "print(\"key1 =\", key1)\n",
    "print(\"key2 =\", key2)\n",
    "print(\"key3 =\", key3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f53e10fc-7b07-4e59-8840-e45fcec86a66",
   "metadata": {},
   "source": [
    "## Python中的转义符\n",
    "\n",
    "在Python字符串中，转义符`\\`用于引入特殊字符。下面列出了一些常用的转义字符：\n",
    "\n",
    "- `\\n`：换行符\n",
    "- `\\t`：制表符，用于添加水平制表空间\n",
    "- `\\\\`：表示一个反斜杠字符`\\`\n",
    "- `\\\"`：表示双引号字符，允许字符串中包含双引号\n",
    "- `\\'`：表示单引号字符，允许字符串中包含单引号\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "057099ed-96c4-460f-be64-aefcd2f0bf80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个换行符的示例:\n",
      "第二行\n",
      "这是一个制表符的示例:\t后面是制表空间\n",
      "这是一个反斜杠的示例:\\\n",
      "包含双引号的字符串: \"这是双引号\"\n",
      "包含单引号的字符串: '这是单引号'\n"
     ]
    }
   ],
   "source": [
    "# 示例代码\n",
    "print(\"这是一个换行符的示例:\\n第二行\")\n",
    "print(\"这是一个制表符的示例:\\t后面是制表空间\")\n",
    "print(\"这是一个反斜杠的示例:\\\\\")\n",
    "print(\"包含双引号的字符串: \\\"这是双引号\\\"\")\n",
    "print(\"包含单引号的字符串: '这是单引号'\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35973c14-8999-4613-8303-3d4fd544abc6",
   "metadata": {},
   "source": [
    "#### 练习题：\n",
    "编写一个Python程序来打印9x9乘法口诀表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "70b4885a-4026-43f8-a134-028f812f5132",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1=1\t\n",
      "1x2=2\t2x2=4\t\n",
      "1x3=3\t2x3=6\t3x3=9\t\n",
      "1x4=4\t2x4=8\t3x4=12\t4x4=16\t\n",
      "1x5=5\t2x5=10\t3x5=15\t4x5=20\t5x5=25\t\n",
      "1x6=6\t2x6=12\t3x6=18\t4x6=24\t5x6=30\t6x6=36\t\n",
      "1x7=7\t2x7=14\t3x7=21\t4x7=28\t5x7=35\t6x7=42\t7x7=49\t\n",
      "1x8=8\t2x8=16\t3x8=24\t4x8=32\t5x8=40\t6x8=48\t7x8=56\t8x8=64\t\n",
      "1x9=9\t2x9=18\t3x9=27\t4x9=36\t5x9=45\t6x9=54\t7x9=63\t8x9=72\t9x9=81\t\n"
     ]
    }
   ],
   "source": [
    "# 练习题答案\n",
    "for i in range(1, 10):  # 外层循环控制行\n",
    "    for j in range(1, i + 1):  # 内层循环控制列\n",
    "        print(f\"{j}x{i}={i*j}\", end=\"\\t\")  # 打印乘法口诀，使用制表符分隔\n",
    "    print()  # 每完成一行打印换行\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd5d58db-f75c-414d-a66b-8a9ecbb7dd02",
   "metadata": {},
   "source": [
    "## 算术运算符\n",
    "\n",
    "Python中的算术运算符包括加（`+`）、减（`-`）、乘（`*`）、除（`/`）、取模（`%`）、整除（`//`）和幂（`**`）。\n",
    "\n",
    "以下是使用这些运算符的一些示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b07e79a5-af0b-4f63-a0f5-f7e2befd1252",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 + 3 = 13\n",
      "10 - 3 = 7\n",
      "10 * 3 = 30\n",
      "10 / 3 = 3.3333333333333335\n",
      "10 % 3 = 1\n",
      "10 // 3 = 3\n",
      "10 ** 3 = 1000\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 3\n",
    "print(f\"{a} + {b} = {a + b}\")  # 加\n",
    "print(f\"{a} - {b} = {a - b}\")  # 减\n",
    "print(f\"{a} * {b} = {a * b}\")  # 乘\n",
    "print(f\"{a} / {b} = {a / b}\")  # 除\n",
    "print(f\"{a} % {b} = {a % b}\")  # 取模\n",
    "print(f\"{a} // {b} = {a // b}\")  # 整除\n",
    "print(f\"{a} ** {b} = {a ** b}\")  # 幂\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b2f8fc9-35f5-4126-b30f-06b08bf3968e",
   "metadata": {},
   "source": [
    "## 逻辑运算符\n",
    "\n",
    "Python中的逻辑运算符包括`and`、`or`和`not`，用于组合布尔值。\n",
    "\n",
    "以下是如何使用逻辑运算符的示例：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "56f0e3a2-4086-42d6-9536-27a3c0b84841",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x and y = False\n",
      "x or y = True\n",
      "not x = False\n"
     ]
    }
   ],
   "source": [
    "# 示例代码\n",
    "x = True\n",
    "y = False\n",
    "print(f\"x and y = {x and y}\")  # 使用and运算符\n",
    "print(f\"x or y = {x or y}\")  # 使用or运算符\n",
    "print(f\"not x = {not x}\")  # 使用not运算符\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c18e342b-bd29-4896-8d40-dd14b483ffbe",
   "metadata": {},
   "source": [
    "### 练习题 1：Hello, Python!\n",
    "编写一个Python程序，输出\"Hello, Python!\"到控制台。\n",
    "\n",
    "### 练习题 2：计算圆的面积\n",
    "给定一个圆的半径，编写一个程序来计算并输出这个圆的面积。提示：圆的面积公式为 πr²。\n",
    "\n",
    "### 练习题 3：列表操作\n",
    "创建一个包含任意五个数字的列表。然后，按照以下要求进行操作：\n",
    "- 添加一个新元素到列表末尾；\n",
    "- 删除列表中的第二个元素；\n",
    "- 打印出修改后的列表。\n",
    "\n",
    "### 练习题 4：字符串逆转\n",
    "编写一个程序，将输入的字符串逆转后输出。例如，输入\"Python\"，输出\"nohtyP\"。\n",
    "\n",
    "### 练习题 5：判断奇偶数\n",
    "输入一个整数，判断该数是奇数还是偶数，并输出结果。\n",
    "\n",
    "### 练习题 6：使用for循环打印乘法口诀表\n",
    "编写一个程序，使用for循环打印9x9乘法口诀表。\n",
    "\n",
    "### 练习题 7：列表解析\n",
    "使用列表解析创建一个列表，其中包含前10个正整数的平方。\n",
    "\n",
    "### 练习题 8：字典操作\n",
    "创建一个字典，包含你最喜欢的三本书及其作者。然后按以下要求进行操作：\n",
    "- 添加一本新书和作者到字典中；\n",
    "- 删除一本书；\n",
    "- 打印出修改后的字典。\n",
    "\n",
    "### 练习题 9：集合运算\n",
    "给定两个集合，打印这两个集合的并集、交集和差集。\n",
    "\n",
    "### 练习题 10：计算BMI\n",
    "编写一个程序，输入身高（米）和体重（千克），计算并输出BMI值。\n",
    "\n",
    "### 练习题 11：字符串格式化\n",
    "编写一个程序，使用字符串格式化输出你的名字和年龄。\n",
    "\n",
    "### 练习题 12：元组操作\n",
    "创建一个元组，包含任意五个元素。尝试修改元组中的一个元素（应引发错误）。\n",
    "\n",
    "### 练习题 13：计算字符串中的字母数量\n",
    "输入一个字符串，计算并输出其中字母的数量。\n",
    "\n",
    "### 练习题 14：找出最大数\n",
    "给定三个不同的整数，编写一个程序来找出并打印最大的数。\n",
    "\n",
    "### 练习题 15：判断闰年\n",
    "编写一个程序，输入一个年份，判断该年份是否是闰年，并输出结果。\n",
    "\n",
    "### 练习题 16：列表去重\n",
    "给定一个包含重复元素的非常长的列表，使用列表到集合的转换来去除列表中的所有重复元素，并打印结果。\n",
    "\n",
    "**示例列表：**\n",
    "```python\n",
    "example_list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 16, 17, 18, 19, 20]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5e48138c-a4df-442d-a089-ccb9cf413a89",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n",
      "圆的面积为：78.53981633974483\n",
      "[1, 3, 4, 5, 6]\n",
      "nohtyP\n",
      "偶数\n",
      "1x1=1\t\n",
      "1x2=2\t2x2=4\t\n",
      "1x3=3\t2x3=6\t3x3=9\t\n",
      "1x4=4\t2x4=8\t3x4=12\t4x4=16\t\n",
      "1x5=5\t2x5=10\t3x5=15\t4x5=20\t5x5=25\t\n",
      "1x6=6\t2x6=12\t3x6=18\t4x6=24\t5x6=30\t6x6=36\t\n",
      "1x7=7\t2x7=14\t3x7=21\t4x7=28\t5x7=35\t6x7=42\t7x7=49\t\n",
      "1x8=8\t2x8=16\t3x8=24\t4x8=32\t5x8=40\t6x8=48\t7x8=56\t8x8=64\t\n",
      "1x9=9\t2x9=18\t3x9=27\t4x9=36\t5x9=45\t6x9=54\t7x9=63\t8x9=72\t9x9=81\t\n",
      "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
      "{'book2': 'author2', 'book3': 'author3', 'book4': 'author4'}\n",
      "并集: {1, 2, 3, 4, 5, 6, 7, 8}\n",
      "交集: {4, 5}\n",
      "差集: {1, 2, 3}\n",
      "BMI: 22.857142857142858\n",
      "My name is John and I am 30 years old.\n",
      "字母数量: 11\n",
      "最大数: 3\n",
      "2020是闰年\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n"
     ]
    }
   ],
   "source": [
    "# 练习题 1：Hello, Python!\n",
    "print(\"Hello, Python!\")\n",
    "\n",
    "# 练习题 2：计算圆的面积\n",
    "import math\n",
    "radius = 5\n",
    "area = math.pi * radius ** 2\n",
    "print(f\"圆的面积为：{area}\")\n",
    "\n",
    "# 练习题 3：列表操作\n",
    "my_list = [1, 2, 3, 4, 5]\n",
    "my_list.append(6)\n",
    "my_list.pop(1)  # 删除索引为1的元素\n",
    "print(my_list)\n",
    "\n",
    "# 练习题 4：字符串逆转\n",
    "string = \"Python\"\n",
    "reversed_string = string[::-1]\n",
    "print(reversed_string)\n",
    "\n",
    "# 练习题 5：判断奇偶数\n",
    "number = 4\n",
    "if number % 2 == 0:\n",
    "    print(\"偶数\")\n",
    "else:\n",
    "    print(\"奇数\")\n",
    "\n",
    "# 练习题 6：使用for循环打印乘法口诀表\n",
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}x{i}={i*j}\", end=\"\\t\")\n",
    "    print()\n",
    "\n",
    "# 练习题 7：列表解析\n",
    "squared_list = [x**2 for x in range(1, 11)]\n",
    "print(squared_list)\n",
    "\n",
    "# 练习题 8：字典操作\n",
    "my_favorites = {'book1': 'author1', 'book2': 'author2', 'book3': 'author3'}\n",
    "my_favorites['book4'] = 'author4'\n",
    "del my_favorites['book1']\n",
    "print(my_favorites)\n",
    "\n",
    "# 练习题 9：集合运算\n",
    "set1 = {1, 2, 3, 4, 5}\n",
    "set2 = {4, 5, 6, 7, 8}\n",
    "print(f\"并集: {set1.union(set2)}\")\n",
    "print(f\"交集: {set1.intersection(set2)}\")\n",
    "print(f\"差集: {set1.difference(set2)}\")\n",
    "\n",
    "# 练习题 10：计算BMI\n",
    "height = 1.75\n",
    "weight = 70\n",
    "bmi = weight / (height ** 2)\n",
    "print(f\"BMI: {bmi}\")\n",
    "\n",
    "# 练习题 11：字符串格式化\n",
    "name = \"John\"\n",
    "age = 30\n",
    "print(f\"My name is {name} and I am {age} years old.\")\n",
    "\n",
    "# 练习题 12：元组操作\n",
    "my_tuple = (1, 2, 3, 4, 5)\n",
    "# my_tuple[1] = 10  # 尝试修改元组，这将引发TypeError\n",
    "\n",
    "# 练习题 13：计算字符串中的字母数量\n",
    "string = \"Hello, Python!\"\n",
    "letter_count = sum(c.isalpha() for c in string)\n",
    "print(f\"字母数量: {letter_count}\")\n",
    "\n",
    "# 练习题 14：找出最大数\n",
    "numbers = [1, 2, 3]\n",
    "print(f\"最大数: {max(numbers)}\")\n",
    "\n",
    "# 练习题 15：判断闰年\n",
    "year = 2020\n",
    "if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n",
    "    print(f\"{year}是闰年\")\n",
    "else:\n",
    "    print(f\"{year}不是闰年\")\n",
    "\n",
    "# 练习题 16：列表去重\n",
    "example_list = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 16, 17, 18, 19, 20]\n",
    "unique_list = list(set(example_list))\n",
    "print(unique_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f956be2f-1443-4710-988b-e4dd822b2ad0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
